#include <iostream>
#include <string>
#include<cstdio>
#include <memory>
#include "UdpServer.hpp"

void Usage(const std::string &proc)
{
    // \r回车符
    std::cout << "\n\rUsage#" << proc << " port[1024+]" << std::endl;
}

std::string Handler(const std::string& str)
{
    std::string  res="Server get a message: ";
    res+=str;
    std::cout<<res<<std::endl;
    return res;
}


std::string ExcuteCommand(const std::string& cmd)
{
    //popen
    FILE*fp=popen(cmd.c_str(),"r");
    if(fp==nullptr)
    {
        perror("popen");
        return "error";
    }
    //读取命令行输出
    std::string result;
    char buffer[4096];
    while(true)
    {
        char*ok=fgets(buffer,sizeof(buffer),fp);
        if(ok==nullptr) break;
        result+=buffer;
    }
    pclose(fp);
    return result;

}
// ./udpserver port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> udpser(new UdpServer(port));

    udpser->Init();
    udpser->Run(Handler);
    std::cout<<"hello RUN after"<<std::endl;

    return 0;
}